#ifndef PAGING_CONTROLLER_H
#define PAGING_CONTROLLER_H

#include <arkui/native_node.h>
#include <arkui/native_node_napi.h>
#include <arkui/native_interface.h>
#include <arkui/native_type.h>
#include <vector>
#include <memory>
#include <functional>
#include <mutex>

// 移除前向声明，因为已经在SDK中定义
// struct ArkUI_NativeNodeAPI_1;

// 加载状态
struct LoadState {
    bool isLoading = false;
    bool isError = false;
    std::string errorMessage;
};

// 组合加载状态
struct CombinedLoadStates {
    LoadState refresh;
    LoadState prepend;
    LoadState append;
};

// 分页数据项基类
template<typename T>
struct PagingItem {
    std::string id;
    T data;
};

// 分页数据容器
template<typename T>
class PagingData {
public:
    std::vector<std::shared_ptr<PagingItem<T>>> items;
    CombinedLoadStates loadStates;
    
    PagingData() = default;
    PagingData(const std::vector<std::shared_ptr<PagingItem<T>>>& items) 
        : items(items) {}
    PagingData(const std::vector<std::shared_ptr<PagingItem<T>>>& items,
               const CombinedLoadStates& states)
        : items(items), loadStates(states) {}
};

// List视图控制器
template<typename T>
class PagingListViewController {
private:
    ArkUI_NodeHandle listNode_ = nullptr;
    ArkUI_NativeNodeAPI_1* nodeAPI_ = nullptr;
    std::vector<std::shared_ptr<PagingItem<T>>> items_;
    std::vector<ArkUI_NodeHandle> itemNodes_;
    CombinedLoadStates loadStates_;
    std::mutex dataMutex_;
    
    // 回调函数
    std::function<ArkUI_NodeHandle(std::shared_ptr<PagingItem<T>>, int)> itemBuilder_;
    std::function<void(int, int)> onDataChanged_;
    std::function<void(int, int)> onDataInserted_;
    std::function<void(int, int)> onDataRemoved_;
    std::function<void(const CombinedLoadStates&)> onLoadStateChanged_;
    
    void createListNode();
    void updateListItems();
    void clearItemNodes();
    
public:
    PagingListViewController();
    ~PagingListViewController();
    
    // 数据操作
    void submitData(const PagingData<T>& data);
    void refresh();
    void retry();
    void invalidate();
    
    // 数据访问
    std::shared_ptr<PagingItem<T>> getItem(int position) const;
    size_t getItemCount() const;
    ArkUI_NodeHandle getListNode() const { return listNode_; }
    
    // 设置回调
    void setItemBuilder(std::function<ArkUI_NodeHandle(std::shared_ptr<PagingItem<T>>, int)> builder);
    void setDataChangeListeners(
        std::function<void(int, int)> onChanged,
        std::function<void(int, int)> onInserted,
        std::function<void(int, int)> onRemoved
    );
    void addLoadStateListener(std::function<void(const CombinedLoadStates&)> listener);
    
    // 滚动控制
    void scrollToPosition(int position);
    void scrollToTop();
    void scrollToBottom();
};

// Grid视图控制器
template<typename T>
class PagingGridViewController {
private:
    ArkUI_NodeHandle gridNode_ = nullptr;
    ArkUI_NativeNodeAPI_1* nodeAPI_ = nullptr;
    std::vector<std::shared_ptr<PagingItem<T>>> items_;
    std::vector<ArkUI_NodeHandle> itemNodes_;
    CombinedLoadStates loadStates_;
    std::mutex dataMutex_;
    
    // Grid配置
    std::string columnsTemplate_ = "1fr 1fr";
    float columnGap_ = 10.0f;
    float rowGap_ = 10.0f;
    
    // 回调函数
    std::function<ArkUI_NodeHandle(std::shared_ptr<PagingItem<T>>, int)> itemBuilder_;
    std::function<void(int, int)> onDataChanged_;
    std::function<void(int, int)> onDataInserted_;
    std::function<void(int, int)> onDataRemoved_;
    
    void createGridNode();
    void updateGridItems();
    void clearItemNodes();
    
public:
    PagingGridViewController();
    ~PagingGridViewController();
    
    // Grid配置
    void setColumnsTemplate(const std::string& template_);
    void setGaps(float columnGap, float rowGap);
    
    // 数据操作
    void submitData(const PagingData<T>& data);
    void refresh();
    void retry();
    
    // 数据访问
    std::shared_ptr<PagingItem<T>> getItem(int position) const;
    size_t getItemCount() const;
    ArkUI_NodeHandle getGridNode() const { return gridNode_; }
    
    // 设置回调
    void setItemBuilder(std::function<ArkUI_NodeHandle(std::shared_ptr<PagingItem<T>>, int)> builder);
    void setDataChangeListeners(
        std::function<void(int, int)> onChanged,
        std::function<void(int, int)> onInserted,
        std::function<void(int, int)> onRemoved
    );
};

#endif // PAGING_CONTROLLER_H